package com.ctcc.serviceImpl;

import com.ctcc.entity.Detail;
import com.ctcc.entity.FormMsg;
import com.ctcc.pojo.FormData;
import com.ctcc.repository.DetailRepository;
import com.ctcc.repository.FormMsgRepository;
import com.ctcc.util.AESEncryptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.kotlin.KotlinModule;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.IOException;

/**
 *
 * Created by Peanut on 2018/2/7.
 */
@Service
public class FormInfoService {

    private AESEncryptor encryptor;
    private  ObjectMapper objectMapper;
    private DetailRepository detailRepository;
    private FormMsgRepository formMsgRepository;
    @Autowired
    public FormInfoService(AESEncryptor encryptor, ObjectMapper objectMapper, DetailRepository detailRepository, FormMsgRepository formMsgRepository) {
        this.encryptor = encryptor;
        this.objectMapper = objectMapper;
        this.detailRepository = detailRepository;
        this.formMsgRepository = formMsgRepository;
    }
    public String  handleWebHook(String data) throws IOException {
        objectMapper.registerModule(new KotlinModule());
        FormData formData = objectMapper.readValue(encryptor.decrypt(data), FormData.class);
        if (formData != null && formData.getSuccess()) {
            //保存表单信息
            saveData(formData);
        }
        return "success";
    }

    @Transactional
    private void saveData(FormData formData) {
            FormMsg formMsg=new FormMsg();

            if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getWidgetMap().get("Te_1").toString())){
                formMsg.setUserName(String.valueOf(formData.getData().getFormInfo().getWidgetMap().get("Te_1").getValue()));
            }
            if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getWidgetMap().get("Te_0").toString())){
                formMsg.setEmail(String.valueOf(formData.getData().getFormInfo().getWidgetMap().get("Te_0").getValue()));
            }
            if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getWidgetMap().get("_S_APPLY").toString())){
                formMsg.setUserSubmit(String.valueOf(formData.getData().getFormInfo().getWidgetMap().get("_S_APPLY").getValue()));
            }
            if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getWidgetMap().get("Da_0").toString())){
                formMsg.setJoinNetTime(String.valueOf(formData.getData().getFormInfo().getWidgetMap().get("Da_0").getValue()));
            }
            if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getWidgetMap().get("De_0").toString())){
                formMsg.setInfo(String.valueOf(formData.getData().getFormInfo().getWidgetMap().get("Da_0").getValue()));
            }
            if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getWidgetMap().get("_S_SERIAL").toString())){
                formMsg.setNumber(String.valueOf(formData.getData().getFormInfo().getWidgetMap().get("_S_SERIAL").getValue()));
            }
            if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getWidgetMap().get("_S_DATE").toString())){
                formMsg.setApplicationTime(String.valueOf(formData.getData().getFormInfo().getWidgetMap().get("_S_DATE").getValue()));
            }
            if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getWidgetMap().get("Ta_0").toString())){
                formMsg.setInfo(String.valueOf(formData.getData().getFormInfo().getWidgetMap().get("Ta_0").getValue()));
            }
            if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getWidgetMap().get("Nu_0").toString())){
                formMsg.setTel(String.valueOf(formData.getData().getFormInfo().getWidgetMap().get("Nu_0").getValue()));
            }
            if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getWidgetMap().get("_S_TITLE").toString())){
                formMsg.setTitle(String.valueOf(formData.getData().getFormInfo().getWidgetMap().get("_S_TITLE").getValue()));
            }
            formMsgRepository.save(formMsg);
            //保存明细

            if(formData.getData().getFormInfo().getDetailMap()!=null){
                for(int i=0;i<formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().size();i++){
                    Detail detail=new Detail();
                    detail.setName("明细"+i);
                    detail.setFormInfoId(formMsg.getTid());
                    if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().get(i).getAc_0())){
                        detail.setDiscount(formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().get(i).getAc_0());
                    }

                    if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().get(i).get_id_())){
                        detail.setTitle(formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().get(i).get_id_());
                    }

                    if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().get(i).getMo_0())){
                        detail.setBeforePrice(formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().get(i).getMo_0());
                    }

                    if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().get(i).getMo_1())){
                        detail.setAfterPrice(formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().get(i).getMo_1());
                    }

                    if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().get(i).getTe_2())){
                        detail.setOther(formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().get(i).getTe_2());
                    }

                    if(StringUtils.isNotEmpty(formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().get(i).get_id_())){
                        detail.setOther(formData.getData().getFormInfo().getDetailMap().get("Dd_0").getWidgetValue().get(i).getTe_2());
                    }
                    detailRepository.save(detail);
                }
            }



        }
}
